﻿using System;
using System.Collections.Generic;
using System.Text;

namespace USERS.CYT369.COM.UTILITY
{
    public class Encrypt
    {

        public static string DesEncrypt(string str, string key, string iv)
        {
            if (!string.IsNullOrEmpty(str))
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
                var des = new System.Security.Cryptography.DESCryptoServiceProvider();

                byte[] Key = Convert.FromBase64String(key);
                byte[] IV = Convert.FromBase64String(iv);

                using (var mStream = new System.IO.MemoryStream())
                {
                    using (var cStream = new System.Security.Cryptography.CryptoStream(mStream, des.CreateEncryptor(Key, IV), System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();
                        return Convert.ToBase64String(mStream.ToArray());
                    }
                }
            }

            return string.Empty;
        }

        public static string DesDecrypt(string str, string key, string iv)
        {
            if (!string.IsNullOrEmpty(str))
            {
                byte[] Key = Convert.FromBase64String(key);
                byte[] IV = Convert.FromBase64String(iv);

                byte[] inputByteArray = Convert.FromBase64String(str);
                var des = new System.Security.Cryptography.DESCryptoServiceProvider();

                using (var mStream = new System.IO.MemoryStream())
                {
                    using (var cStream = new System.Security.Cryptography.CryptoStream(mStream, des.CreateDecryptor(Key, IV), System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();
                        return Encoding.UTF8.GetString(mStream.ToArray());
                    }
                }

            }

            return string.Empty;
        }

    }


}
